package com.lzh.runke.controller.exceltest.poi;

import com.lzh.runke.core.web_base.autoconfigure.WebAutoConfigProperties;
import com.lzh.runke.core.web_base.constant.DateConstant;
import com.lzh.runke.core.web_base.constant.StringConstant;
import com.lzh.runke.core.web_base.exception.BusinessException;
import com.lzh.runke.core.web_base.view.excel.AbstractExcelView;
import com.lzh.runke.entity.HeaderCell;
import com.lzh.runke.util.ExportUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 静态与动态表头实现
 * 导出模板
 *
 * @author LiuZiHao
 * @date 创建时间:2023年06月13日 17:17:38
 */
@Component("CommonViewTest")
public class CommonView extends AbstractExcelView<CommonView> {
    @Resource
    private WebAutoConfigProperties webAutoConfigProperties;
    @Resource
    private ResourceLoader resourceLoader;
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        if (workbook == null) {
            throw new BusinessException("ERROR:没有CommonView.xlsx文件(workbook==null)");
        }
        Sheet sheet = workbook.getSheetAt(0);
        if (sheet == null) {
            sheet = workbook.createSheet("Sheet1");
        }
        Font font = workbook.createFont();//获取字体设置对象
        font.setFontName("宋体");
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(141, 180, 226), new DefaultIndexedColorMap());
        //第一行单元格样式
        XSSFCellStyle rowStyle = ExportUtil.createXCellStyle(workbook, font, xssfColor, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, BorderStyle.THIN);
        //数据行单元格样式
        XSSFCellStyle cellDataStyle = ExportUtil.createXCellStyle(workbook, font, null, HorizontalAlignment.CENTER, VerticalAlignment.CENTER, BorderStyle.THIN);

        //表头
        int rowIndex = 0;
        Map<String, Object> extraMap = (Map<String, Object>) model.get("extra");
        List<Object> dynamicPart = (List<Object>) extraMap.get("header");

        ArrayList<HeaderCell> headerList = new ArrayList<>();
        headerList.add(HeaderCell.builder().value("静态表头").build());
        headerList.add(HeaderCell.builder().value("静态表头").build());
        if (CollectionUtils.isNotEmpty(dynamicPart)) {
            List<HeaderCell> collect = dynamicPart
                    .stream()
                    .map(item ->
                            HeaderCell.builder()
                                    .key(null)
                                    .value(null).build()
                    )
                    .collect(Collectors.toList());
            headerList.addAll(collect);
        }
        Row row = sheet.getRow(rowIndex);
        if (ObjectUtils.isEmpty(row)) {
            row = sheet.createRow(rowIndex);
        }
        int headerLength = headerList.size();
        for (int i = 0; i < headerLength; i++) {
            addCell(row, i, headerList.get(i).getValue(), rowStyle);
            sheet.setColumnWidth(i, (int) ((22 + 0.72) * 256));
        }

        //数据行
        List<Object> dataList = (List<Object>) model.get("data");
        //动态部分长度
        int dynamicHeaderTotal = dynamicPart.size();
        if (CollectionUtils.isNotEmpty(dataList)) {
            int colIndex;
            for (Object data : dataList) {
                colIndex = -1;
                rowIndex += 1;
                row = sheet.getRow(rowIndex);
                if (ObjectUtils.isEmpty(row)) {
                    row = sheet.createRow(rowIndex);
                }
                addCell(row, ++colIndex, String.valueOf(rowIndex), cellDataStyle);
                /*添加数据*/
                addCell(row, ++colIndex, data.toString(), cellDataStyle);
                addCell(row, ++colIndex, data.toString() == null ? StringConstant.CH_DASH : data.toString(), cellDataStyle);
                addCell(row, ++colIndex, StringUtils.isBlank(data.toString()) ? StringConstant.CH_DASH : data.toString(), cellDataStyle);
                addCell(row, ++colIndex, (data.toString() == null || StringUtils.isBlank(data.toString().toLowerCase())) ? StringConstant.CH_DASH : data.toString().toLowerCase(), cellDataStyle);
                /**
                 * 添加动态表头的数据
                 * 如果顺序位置个数与表头匹配，直接循环addCell
                 */
//               List<Object> dataList = data.getDataList();
//                for (Object obj : dataList) {
//                    addCell(row, ++colIndex, obj.getValue() == null ? StringConstant.CH_DASH : obj.getValue(), cellDataStyle);
//                }
                /**
                 *否则需要根据一个key来匹配对应的值
                 *
                 * */
//                List<Object> dataList = data.getDataList();
//                Map<String, String> dataMap = dataList
//                        .stream()
//                        .collect(Collectors.toMap(BaseEntity::getId,
//                                obj::getValue));
//                for (int i = 0; i < dynamicHeaderTotal; i++) {
//                    String key = headerList.get(colIndex + 1).getKey();
//                    String dataValue = dataMap.get(key);
//                    addCell(row, ++colIndex, dataValue == null ? StringConstant.CH_DASH : dataValue, cellDataStyle);
//                }

            }
        }
        Object spec = (Object) model.get("spec");
        String fileName = "文件名" + DateFormatUtils.format(new Date(), DateConstant.FMT_YMD);
        setExportFilename(httpServletResponse, fileName + XLSX);

    }

    /**
     * 合并单元格
     */
}
